package com.yinggu.servlet;

import com.yinggu.pojo.KUser;
import com.yinggu.service.UserService;
import com.yinggu.utils.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userList")
public class UserServlet extends HttpServlet {
    UserService service = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        final String key = req.getParameter("key");
        final String currentPage = req.getParameter("currentPage");
        final String pageSize = req.getParameter("pageSize");
        Page page = new Page(1,5,service.getCount(key));
        if (currentPage != null && pageSize != null) {
            page.setCurrentPage(Integer.parseInt(currentPage));
            page.setPageSize(Integer.parseInt(pageSize));
        }
        final List<KUser> users = service.getUsers(key, page);
        req.setAttribute("list", users);
        req.setAttribute("key", key);
        req.setAttribute("page", page);
        req.getRequestDispatcher("/admin/userList.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
